package com.example.controller;

import com.example.dao.UserMapper;
import com.example.entity.User;
import com.example.log.annotation.LogOperation;
import com.example.model.api.Result;
import com.example.feign.AuthFeignClient;
import com.example.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;


@Slf4j
@Tag(name = "测试fegin")
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthFeignClient userFeignClient;
    @Autowired
    private UserService userService;

    /**
     * 该接口放行
     * .requestMatchers(HttpMethod.GET, "/demo/abc").permitAll()
     * @return
     */

    @Operation(summary = "延时普通abc请求")
    @PostMapping(path = "abc")
    @LogOperation("bac_test")
    public Result<String> postabc(@RequestBody User map) {
//    public Result<String> postabc(@RequestBody Map<String, Object> map) {
        List<User> users = userMapper.selectList(null);
        System.out.println(users);

        System.out.println("------------");
        List<User> listUsers = userService.getListUsers();
        System.out.println(listUsers);



        Result<String> result = userFeignClient.openfignDemo();
        System.out.println(result.getData());

//        UserDetail user = SecurityUser.getUser();
//        System.out.println(user);
        return new Result<String>().ok("abc");
    }

    @Operation(summary = "延时普通abc请求")
    @GetMapping(path = "abc")
    @LogOperation("bac_test")
    public Result<String> abc() {
        List<User> users = userMapper.selectList(null);
        System.out.println(users);

        System.out.println("------------");
        List<User> listUsers = userService.getListUsers();
        System.out.println(listUsers);

        Result<String> result = userFeignClient.openfignDemo();
        System.out.println(result.getData());

//        UserDetail user = SecurityUser.getUser();
//        System.out.println(user);
        return new Result<String>().ok("abc");
    }






    //    @PreAuthorize("hasAuthority('SCOPE_message.read')")
    @GetMapping("resourceone")
    public String getResource() {
        return "Resource SCOPE_message.read";
    }
    @GetMapping
    @PreAuthorize("hasRole('abc')")
    public String demo1() {
        return "hello demo1";
    }

    /**
     * 认证后即可登录
     * @return
     */
    @GetMapping("two")
    public String demo2() {
        return "hello demo2";
    }
    @GetMapping("hello")
    public Mono<String> gethello() {
        return Mono.just("Hello, me!");
    }
    @GetMapping("authenticationhello")
    public Mono<Authentication> authenticationhello() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return Mono.just(authentication);
    }
    /**
     * 认证后，并满足授权
     * @return
     */
    @GetMapping("three")
    @PreAuthorize("hasAuthority('ROLE_USER')")
    public String demo3() {
        return "hello demo3";
    }



}